package com.shitou.shitoumember.intercetpor;

import com.shitou.shitoucommons.annotation.MemberAuthorith;
import com.shitou.shitoucommons.interceptor.LoginInterceptor;
import com.shitou.shitoucommons.opfeign.MemberFeign;
import com.shitou.shitoucommons.utils.RespBean;
import com.shitou.shitoumember.entity.MemberEntity;
import com.shitou.shitoumember.utils.InterceptorUtils;
import com.shitou.shitoumember.utils.MemberContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import com.shitou.shitoucommons.annotation.CheckUserLogin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@SuppressWarnings("all")
@Slf4j
@Component
public class MemberLoginInterceptor extends LoginInterceptor implements HandlerInterceptor {


    @Autowired
    private InterceptorUtils interceptorUtils;


    /**
     * 登录拦截，判断用户是否登录
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 登录校验
        boolean isLogin = checkLogin(handler, request, response, null);

        // 权限校验
        boolean authority = checkAuthority(handler, request, response, null);

        return isLogin && authority;
    }


    @Override
    public boolean checkAuthority(Object handler, HttpServletRequest request, HttpServletResponse response, MemberFeign memberFeign) {

        if( handler instanceof  HandlerMethod ){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            MemberAuthorith authorith = handlerMethod.getMethodAnnotation(MemberAuthorith.class);
            if ( authorith == null ){
                return true;
            }

            // TODO 权限校验

            // TODO 烂尾，，集成不了了

        }

        return true;
    }


    @Override
    public boolean checkLogin(Object handler, HttpServletRequest request, HttpServletResponse response, MemberFeign memberFeign) {
        // 如果handler是一个方法，那么进行强转和注解获取
        if ( handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
            CheckUserLogin loginAnn = hm.getMethodAnnotation(CheckUserLogin.class);
            if (loginAnn == null) {
                // 该接口不需要登录
                return true;
            }
            boolean value = loginAnn.value();
            if (!value) {
                return true;
            }
            // 从cookie或者session中获取用户
            MemberEntity user = interceptorUtils.getUserByCookie(request, response, "memberTicket");
            if (user == null) {
                user = interceptorUtils.getUserBySession(request, response, "memberTicket");
            }
            // 如果获取的用户为null，那么对该接口进行拦截
            if (user == null) {
                super.render(response, RespBean.error(402, "未登录"));
                return false;
            }
            // 存入用户的本地缓存中
            MemberContext.setObject(user);
        }
        return true;
    }
}
